local yinzui = fk.CreateSkill{
    name = "ym2__yinzui",
    tags = {Skill.Quest,Skill.Compulsory}
}
Fk:loadTranslationTable{
    ["ym2__yinzui"] = "荫罪",
    [":ym2__yinzui"] = "使命技，锁定技，你获得过牌的回合结束时，你失去所有其他技能并选择一项："..
    "<br>1.获得“复难”并发动“连讨”；"..
    "<br>2.获得“绝情”并发动“剑合”；"..
    "<br>3.获得“卫境”并发动“烈誓”。"..
    "<br>成功：当你因此杀死角色时，你获得“鸿图”。"..
    "<br>失败：当你进入濒死状态时，你获得“战绝”。",
    ["#ym2__yinzui1"] = "1.获得“复难”并发动“连讨”；",
    ["#ym2__yinzui2"] = "2.获得“绝情”并发动“剑合”；",
    ["#ym2__yinzui3"] = "3.获得“卫境”并发动“烈誓”。",

    ["$ym2__yinzui1"] = "吾有三罪，未能除黄皓、制伯约、守国土。",
    ["$ym2__yinzui2"] = "心怀父诫，方不愧父亲荫庇。",
    ["$ym2__yinzui3"] = "数罪当论，吾愧对先帝恩惠。",

    ["$ym2__funan_ym2__zhugezhan"] = "望陛下闻谏，亲贤臣，远小人。",
    ["$ym2__jueqing_ym2__zhugezhan"] = "非吾绝情，实伯约一意孤行啊！",
    ["$ym2__weijing_ym2__zhugezhan"] = "守国土，护国祠，吾誓不后退！",

    ["$ym2__liantao_ym2__zhugezhan"] = "国贼！今日吾不杀你，何以报先帝之恩？",
    ["$ym2__jianhe_ym2__zhugezhan"] = "今时国微民贫，众将有心无力，望伯约举虎符而慎之！",
    ["$ym2__lieshi_ym2__zhugezhan"] = "炎汉昭昭，先烈血染，怎可拱手他人！",

    ["$ym2__hongtu_ym2__zhugezhan"] = "今后方已定，当大展宏图，北伐讨贼！",
    ["$ym2__zhanjue_ym2__zhugezhan"] = "此刻，唯有死战，安能言降！",

    ["$ym2__feijun_ym2__zhugezhan"] = "行此计，如神兵天降，可望故都。",
    ["$ym2__qianxi_ym2__zhugezhan"] = "行此计，纵坚壁清野，亦破曹军。",
}
yinzui:addEffect(fk.TurnEnd,{
    audio_index = 1,
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(yinzui.name) then
            return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
                for _, move in ipairs(e.data) do
                    if move.to == player and move.toArea == Player.Hand then
                        return true
                    end
                end
            end, Player.HistoryTurn) > 0
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choice = room:askToChoice(player,{
            choices = {"#ym2__yinzui1","#ym2__yinzui2","#ym2__yinzui3"},
            skill_name = yinzui.name
        })
        event:setCostData(self,choice)
        return true
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local cost_data = event:getCostData(self)
        local skills = {}
        for _, skill in ipairs(player.player_skills) do
            if skill.name ~= yinzui.name and skill:isPlayerSkill(player) then
                table.insert(skills, skill.name)
            end
        end
        room:handleAddLoseSkills(player,"-"..table.concat(skills,"|-"))
        if cost_data == "#ym2__yinzui1" then
            room:handleAddLoseSkills(player,"ym2__funan")
            if #room:getOtherPlayers(player, false) > 0 then
                local tos = room:askToChoosePlayers(player, {
                    min_num = 1,
                    max_num = 1,
                    targets = room:getOtherPlayers(player, false),
                    skill_name = "ym2__liantao",
                    prompt = "#ym2__liantao-choose",
                    cancelable = false,
                })
                if #tos > 0 then
                    room:notifySkillInvoked(player,"ym2__liantao","offensive")
                    player:broadcastSkillInvoke("ym2__liantao")
                    local to = tos[1]
                    local choice = room:askToChoice(to, {
                        choices = {"red", "black"},
                        skill_name = "ym2__liantao",
                        prompt = "#ym2__liantao-choice:"..player.id,
                    })
                    local events, damage
                    local draw3, breakloop, x = true, false, 0
                    local event_id = room.logic.current_event_id
                    while not player.dead and not player:isKongcheng() do
                        local duel = Fk:cloneCard("duel")
                        duel.skillName = "ym2__liantao"
                        local cards = table.filter(player:getCardIds("h"), function(id)
                            duel.subcards = {}
                            duel:addSubcard(id)
                            return duel:getColorString() == choice and player:canUseTo(duel, to)
                        end)
                        if #cards == 0 then break end
                        local card = room:askToCards(player, {
                            min_num = 1,
                            max_num = 1,
                            include_equip = false,
                            skill_name = "ym2__liantao",
                            pattern = tostring(Exppattern{ id = cards }),
                            prompt = "#ym2__liantao-duel::"..to.id..":"..choice,
                            cancelable = false,
                        })
                        local use = room:useVirtualCard("duel", card, player, to, "ym2__liantao")
                        if not use then break end
                        if use.damageDealt then
                            draw3 = false
                        end
                        if player.dead then return end
                        events = room.logic.event_recorder[GameEvent.Damage] or Util.DummyTable
                        for i = #events, 1, -1 do
                            local e = events[i]
                            if e.id <= event_id then break end
                            damage = e.data
                            if damage.dealtRecorderId and damage.from == player and damage.card == use.card then
                            x = x + damage.damage
                            end
                        end
                        if target.dead then break end
                        events = room.logic.event_recorder[GameEvent.Dying] or Util.DummyTable
                        for i = #events, 1, -1 do
                            local e = events[i]
                            if e.id <= event_id then break end
                            if e.data.who == player or e.data.who == to then
                                breakloop = true
                                break
                            end
                        end
                        if breakloop then break end
                        event_id = room.logic.current_event_id
                    end
                    if draw3 then
                        room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, 3)
                        room:setPlayerMark(player, "ym2__liantao_prohibit-turn", 1)
                        room:drawCards(player, 3, "ym2__liantao")
                    elseif x > 0 then
                        room:drawCards(player, x, "ym2__liantao")
                    end
                end
            end
        elseif cost_data == "#ym2__yinzui2" then
            room:handleAddLoseSkills(player,"ym2__jueqing")
            local dat, result = room:askToUseActiveSkill(player,{
                skill_name = "ym2__jianhe",
                cancelable = false,
                prompt = "#ym2__jianhe",
                skip = true,
            })
            if dat and result then
                room:notifySkillInvoked(player,"ym2__jianhe","offensive")
                player:broadcastSkillInvoke("ym2__jianhe")
                local to = result.targets[1]
                room:addTableMark(player, "ym2__jianhe-phase", to.id)
                local n, type = #result.cards, Fk:getCardById(result.cards[1]):getTypeString()
                room:recastCard(result.cards, player, "ym2__jianhe")
                if to.dead then return end
                if #to:getCardIds("he") >= n then
                    local cards = room:askToCards(to, {
                        min_num = n,
                        max_num = n,
                        include_equip = true,
                        skill_name = "ym2__jianhe",
                        pattern = ".|.|.|.|.|"..type,
                        prompt = "#ym2__jianhe-choose:::"..n..":"..type,
                        cancelable = true,
                    })
                    if #cards > 0 then
                        room:recastCard(cards, to, "ym2__jianhe")
                        return
                    end
                end
                room:damage{
                    from = player,
                    to = to,
                    damage = 1,
                    damageType = fk.ThunderDamage,
                    skillName = "ym2__jianhe",
                }
            end
        else
            room:handleAddLoseSkills(player,"ym2__weijing")
            local dat, result = room:askToUseActiveSkill(player,{
                skill_name = "ym2__lieshi",
                cancelable = false,
                prompt = "#ym2__lieshi",
                skip = true,
            })
            if dat and result then
                room:notifySkillInvoked(player,"ym2__lieshi","offensive")
                player:broadcastSkillInvoke("ym2__lieshi")
                local choice = result.interaction
                local to = player
                for i = 1, 2, 1 do
                    if i == 2 then
                        if player.dead then return end
                        to = room:askToChoosePlayers(player, {
                            min_num = 1,
                            max_num = 1,
                            targets = room.alive_players,
                            skill_name = "ym2__lieshi",
                            prompt = "#ym2__lieshi-choose",
                            cancelable = false,
                        })[1]
                        local choices, all_choices = {}, {"ym2__lieshi_damage", "ym2__lieshi_slash", "ym2__lieshi_jink"}
                        if not table.contains(to.sealedSlots, Player.JudgeSlot) then
                            table.insert(choices, "ym2__lieshi_damage")
                        end
                        if table.find(to:getCardIds("h"), function (id)
                        return Fk:getCardById(id).trueName == "slash" and not to:prohibitDiscard(id)
                        end) then
                            table.insert(choices, "ym2__lieshi_slash")
                        end
                        if table.find(to:getCardIds("h"), function (id)
                        return Fk:getCardById(id).trueName == "jink" and not to:prohibitDiscard(id)
                        end) then
                            table.insert(choices, "ym2__lieshi_jink")
                        end
                        table.removeOne(choices, choice)
                        if #choices == 0 then return end
                        choice = room:askToChoice(to, {
                            choices = choices,
                            skill_name = "ym2__lieshi",
                            prompt = "#ym2__lieshi-choice:"..player.id,
                            all_choices = all_choices,
                        })
                    end
                    if choice == "ym2__lieshi_damage" then
                        room:abortPlayerArea(to, Player.JudgeSlot)
                        if not to.dead then
                        room:damage{
                            from = player,
                            to = to,
                            damage = 1,
                            damageType = fk.FireDamage,
                            skillName = "ym2__lieshi",
                        }
                        end
                    elseif choice == "ym2__lieshi_slash" then
                        local cards = table.filter(to:getCardIds("h"), function (id)
                        return Fk:getCardById(id).trueName == "slash" and not to:prohibitDiscard(id)
                        end)
                        if #cards > 0 then
                        room:throwCard(cards, "ym2__lieshi", to, to)
                        end
                    elseif choice == "ym2__lieshi_jink" then
                        local cards = table.filter(to:getCardIds("h"), function (id)
                        return Fk:getCardById(id).trueName == "jink" and not to:prohibitDiscard(id)
                        end)
                        if #cards > 0 then
                        room:throwCard(cards, "ym2__lieshi", to, to)
                        end
                    end
                end
            end
        end
    end,
})
yinzui:addEffect("prohibit", {
    prohibit_use = function(self, player, card)
        return player:getMark("ym2__liantao_prohibit-turn") > 0 and card.trueName == "slash"
    end,
})
yinzui:addEffect(fk.Deathed,{
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(yinzui.name) and data.killer == player then
            local e = player.room.logic:getCurrentEvent()
            for i = 1, 8, 1 do
                if e then
                    if e.event == GameEvent.SkillEffect then
                        local use = e.data
                        if use and use.skill.name == yinzui.name and use.who == player then
                            return true
                        end
                    end
                    e = e.parent
                else
                    break
                end
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player,yinzui.name,"special")
        player:broadcastSkillInvoke(yinzui.name,2)
        room:updateQuestSkillState(player, yinzui.name)
        room:invalidateSkill(player, yinzui.name)
        room:handleAddLoseSkills(player,"ym2__hongtu")
    end,
})
yinzui:addEffect(fk.EnterDying, {
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(yinzui.name) and not player:getQuestSkillState(yinzui.name)
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player,yinzui.name,"negative")
        player:broadcastSkillInvoke(yinzui.name,3)
        room:updateQuestSkillState(player, yinzui.name, true)
        room:invalidateSkill(player, yinzui.name)
        room:handleAddLoseSkills(player,"ym2__zhanjue")
    end,
})

return yinzui